package Movimientos;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import Naves.*;
import Armas.Bala;


public class MovJugador extends Movimiento implements KeyListener{	
	
	protected MovBala mBala;
	protected boolean disparo;
	protected NaveJugador nave;
	
	//Constructor 
	
	 public MovJugador() {
		 
		super();
		disparo = false;
	 }
	
	public void setMovimientoBala(MovBala mov) {
		
		mBala = mov;
	}
	
	public void setJugador(NaveJugador j){
		nave = j;
	}
	
	public void keyPressed(KeyEvent e) {

		int vel = 0;
		Point pos = null;
		pos = nave.getLocation();
		vel = nave.obtenerVelocidad();
		switch(e.getKeyCode()){
		 case KeyEvent.VK_UP :{
			int v = pos.y - vel;
			pos.setLocation(pos.x, v);
		break;
		}
		case KeyEvent.VK_DOWN :{
			int v = pos.y + vel;
			pos.setLocation(pos.x, v);
		break;
		}
		case KeyEvent.VK_LEFT :{
			int v = pos.x - vel;
			pos.setLocation(v, pos.y);
		break;
		}
		case KeyEvent.VK_RIGHT :{
			int v = pos.x + vel;
			pos.setLocation(v, pos.y);
		break;
		  }
	    }
	   nave.mover(pos);
	   getGUI().repintarComponentes();
	 }
		
	public void keyReleased(KeyEvent e) {
	
		  
			     //Tratamiento del evento
			    
			    if (e.getKeyCode() == KeyEvent.VK_SPACE)  {

			         //Ejecutar dispar
				     Bala [] array = nave.disparar(nivel,getGUI());
				     this.getGUI().agregarComponente(array);
		             for (int i=0; i<array.length; i++) {
		      	        mBala.addComponente(array[i]);
		                 }
		             
		             if (mBala.estaSuspendido()) {
		            	mBala.resumir();
		             }
		             
		             if (!disparo) {
					      mBala.empezar();
					      disparo = true;
		               }
		          }
	       }

	public void keyTyped(KeyEvent e) {}
}
